﻿using System;
using System.Collections.Generic;
using System.Text;
using SharpDiary.Core.Data;
using SharpDiary.Core.Utility;

namespace SharpDiary.Core.ORMapping
{
    /// <summary>
    /// 任务信息。
    /// </summary>
    [TableInfoAttribute("TaskInfo")]
    public class TaskInfo : DbMappingObjectBase
    {
        #region Fields & Properties

        private Int64 _id = -1;

        [FieldMapping(true, System.Data.DbType.Int64, true, true)]
        public Int64 Id
        {
            get { return _id; }
            set { _id = value; }
        }

        private string _name = string.Empty;

        [FieldMapping(false, System.Data.DbType.String, false, false)]
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        private string _description = string.Empty;

        [FieldMapping(false, System.Data.DbType.String, false, false)]
        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }

        private DateTime _startDatetime = DateTime.Now;

        [FieldMapping(false, System.Data.DbType.DateTime, false, false)]
        public DateTime StartDatetime
        {
            get { return _startDatetime; }
            set { _startDatetime = value; }
        }

        private DateTime _endDatetime = DateTime.Now;

        [FieldMapping(false, System.Data.DbType.DateTime, false, false)]
        public DateTime EndDatetime
        {
            get { return _endDatetime; }
            set { _endDatetime = value; }
        }

        private Int64 _status = 0;

        [FieldMapping(false, System.Data.DbType.Int64, false, false)]
        public Int64 Status
        {
            get { return _status; }
            set { _status = value; }
        }

        private Int64 _parentTaskId = -1;

        /// <summary>
        /// 父任务编码。
        /// </summary>
        [FieldMapping(false, System.Data.DbType.Int64, false, false)]
        public Int64 ParentTaskId
        {
            get { return _parentTaskId; }
            set { _parentTaskId = value; }
        }

        private Int64 _priority = 0;

        [FieldMapping(false, System.Data.DbType.Int64, false, false)]
        public Int64 Priority
        {
            get { return _priority; }
            set { _priority = value; }
        }

        private string _place = string.Empty;

        [FieldMapping(false, System.Data.DbType.String, false, false)]
        public string Place
        {
            get { return _place; }
            set { _place = value; }
        }

        #endregion

        #region Constructors

        public TaskInfo() { }

        #endregion

        #region Methods

        /// <summary>
        /// 获取所有任务。
        /// </summary>
        /// <returns></returns>
        public static List<TaskInfo> GetAllTasks()
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendLine("select * from TaskInfo");

            return DbObjectMappingHelper.QueryObjectList<TaskInfo>(sql.ToString());
        }

        /// <summary>
        /// 取所有任务数量。
        /// </summary>
        /// <returns>总任务数量</returns>
        public static int GetTotalTaskCount()
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendLine("select count(*) as FCount from TaskInfo");

            DataAccessService das = Utils.GetDataAccessServiceInstance();
            object result = das.ExecuteScalar(sql.ToString());

            return (result != null && result != DBNull.Value ? Convert.ToInt32(result) : 0);
        }

        /// <summary>
        /// 获取所有子任务。
        /// </summary>
        /// <returns></returns>
        public List<TaskInfo> GetSubTasks()
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendLine("select * from TaskInfo");
            sql.AppendLine("where ParentTaskId = @id");

            DataAccessService das = Utils.GetDataAccessServiceInstance();
            return DbObjectMappingHelper.QueryObjectList<TaskInfo>(sql.ToString(),
                das.CreateParameter("@id", System.Data.DbType.Int64, _id));
        }

        /// <summary>
        /// 获取指定编号的任务信息。
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private static TaskInfo GetTaskInfoById(Int64 id)
        {
            if (id < 0) { return null; }

            StringBuilder sql = new StringBuilder();
            sql.AppendLine("select * from TaskInfo where Id = @id");

            DataAccessService das = Utils.GetDataAccessServiceInstance();
            return DbObjectMappingHelper.Query<TaskInfo>(sql.ToString(),
                das.CreateParameter("@id", System.Data.DbType.Int64, id));
        }

        #endregion
    }
}
